using System;
using System.Collections.Generic;
using CodePlex.TfsLibrary.ClientEngine;
using CodePlex.TfsLibrary.ObjectModel;
using CodePlex.TfsLibrary.RepositoryWebSvc;

namespace CodePlex.UnitTestUtility
{
    public class StubTfsEngine : ITfsEngine
    {
        public List<SourceItemCallbackData> Add__CallbackData;
        public string Add_LocalPath;
        public bool Add_Recursive;
        bool attemptAutoMerge;
        public string Checkout_LocalPath;
        public bool Checkout_Recursive;
        public string Checkout_ServerPath;
        public string Checkout_TfsUrl;
        public VersionSpec Checkout_Version;
        public List<SourceItem> Commit__CommitList;
        public int Commit__Result = 42;
        public bool Commit_Called = false;
        public string Commit_LocalPath;
        public string Commit_Message;
        CredentialsCallback credentialsCallback;
        public List<SourceItemCallbackData> Delete__CallbackData;
        public bool Delete_Force;
        public string Delete_LocalPath;
        public string Diff_LocalPath;
        public bool Diff_Recursive;
        public string GetFolderInfo_Directory;
        public TfsFolderInfo GetFolderInfo_Result;
        public SourceItem GetSourceItem__Result = new SourceItem();
        public string GetSourceItem_LocalPath;
        string ignoreFile;
        public bool IsFolderTracked__Result = true;
        public string IsFolderTracked_Directory;
        public bool List_Recursive;
        public string List_ServerPath;
        public string List_TfsUrl;
        public VersionSpec List_Version;
        public string Log_LocalPath;
        public int Log_MaxCount;
        public VersionSpec Log_VersionFrom;
        public VersionSpec Log_VersionTo;
        public List<SourceItemCallbackData> Resolve__CallbackData;
        public string Resolve_LocalPath;
        public bool Resolve_Recursive;
        public List<SourceItemCallbackData> Revert__CallbackData;
        public string Revert_LocalPath;
        public bool Revert_Recursive;
        public List<SourceItemCallbackData> Status__CallbackData;
        public bool Status_IncludeServer;
        public string Status_LocalPath;
        public bool Status_Recursive;
        public string Syncup_LocalPath;
        public string Update_LocalPath;
        public bool Update_Recursive;
        public VersionSpec Update_Version;

        public StubTfsEngine()
        {
            GetFolderInfo_Result = new TfsFolderInfo("http://tfsUrl", "$/Server/Path/");

            Add__CallbackData = CreateSampleData();
            Delete__CallbackData = CreateSampleData();
            Resolve__CallbackData = CreateSampleData();
            Revert__CallbackData = CreateSampleData();
            Status__CallbackData = CreateSampleData();

            Commit__CommitList = new List<SourceItem>();
            Commit__CommitList.Add(new SourceItem());
        }

        public bool AttemptAutoMerge
        {
            get { return attemptAutoMerge; }
            set { attemptAutoMerge = value; }
        }

        public CredentialsCallback CredentialsCallback
        {
            get { return credentialsCallback; }
            set { credentialsCallback = value; }
        }

        public string IgnoreFile
        {
            get { return ignoreFile; }
            set { ignoreFile = value; }
        }

        public void Add(string localPath,
                        bool recursive,
                        SourceItemCallback callback)
        {
            Add_LocalPath = localPath;
            Add_Recursive = recursive;

            if (callback != null)
                foreach (SourceItemCallbackData data in Add__CallbackData)
                    callback(data.SourceItem, data.SourceItemResult);
        }

        public void Checkout(string tfsUrl,
                             string serverPath,
                             string localPath,
                             bool recursive,
                             VersionSpec version,
                             SourceItemCallback callback)
        {
            Checkout_TfsUrl = tfsUrl;
            Checkout_ServerPath = serverPath;
            Checkout_LocalPath = localPath;
            Checkout_Recursive = recursive;
            Checkout_Version = version;

            if (callback != null)
                callback(new SourceItem(), SourceItemResult.S_Ok);
        }

        public int Commit(string localPath,
                          string message,
                          SourceItemCallback callback)
        {
            Commit_Called = true;
            Commit_LocalPath = localPath;
            Commit_Message = message;

            if (callback != null)
                foreach (SourceItem item in Commit__CommitList)
                    callback(item, SourceItemResult.S_Ok);

            return Commit__Result;
        }

        static List<SourceItemCallbackData> CreateSampleData()
        {
            List<SourceItemCallbackData> result = new List<SourceItemCallbackData>();
            result.Add(new SourceItemCallbackData(new SourceItem(), SourceItemResult.S_Ok));
            return result;
        }

        public void Delete(string localPath,
                           bool force,
                           SourceItemCallback callback)
        {
            Delete_LocalPath = localPath;
            Delete_Force = force;

            if (callback != null)
                foreach (SourceItemCallbackData data in Delete__CallbackData)
                    callback(data.SourceItem, data.SourceItemResult);
        }

        public void Diff(string localPath,
                         bool recursive,
                         DiffCallback callback)
        {
            Diff_LocalPath = localPath;
            Diff_Recursive = recursive;

            if (callback != null)
                callback(null, null, null, null, null, SourceItemResult.S_Ok);
        }

        public TfsFolderInfo GetFolderInfo(string directory)
        {
            GetFolderInfo_Directory = directory;

            return GetFolderInfo_Result;
        }

        public SourceItem GetSourceItem(string localPath)
        {
            GetSourceItem_LocalPath = localPath;

            return GetSourceItem__Result;
        }

        public SourceItem[] GetSourceItems(string directory)
        {
            throw new NotImplementedException();
        }

        public bool IsFileTracked(string filename)
        {
            throw new NotImplementedException();
        }

        public bool IsFolderTracked(string directory)
        {
            IsFolderTracked_Directory = directory;

            return IsFolderTracked__Result;
        }

        public void List(string tfsUrl,
                         string serverPath,
                         bool recursive,
                         VersionSpec version,
                         SourceItemCallback callback)
        {
            List_TfsUrl = tfsUrl;
            List_ServerPath = serverPath;
            List_Recursive = recursive;
            List_Version = version;

            if (callback != null)
                callback(new SourceItem(), SourceItemResult.S_Ok);
        }

        public void Log(string localPath,
                        VersionSpec versionFrom,
                        VersionSpec versionTo,
                        int maxCount,
                        LogCallback callback)
        {
            Log_LocalPath = localPath;
            Log_VersionFrom = versionFrom;
            Log_VersionTo = versionTo;
            Log_MaxCount = maxCount;

            if (callback != null)
                callback(new LogItem(), SourceItemResult.S_Ok);
        }

        public void Resolve(string localPath,
                            bool recursive,
                            SourceItemCallback callback)
        {
            Resolve_LocalPath = localPath;
            Resolve_Recursive = recursive;

            if (callback != null)
                foreach (SourceItemCallbackData data in Resolve__CallbackData)
                    callback(data.SourceItem, data.SourceItemResult);
        }

        public void Revert(string localPath,
                           bool recursive,
                           SourceItemCallback callback)
        {
            Revert_LocalPath = localPath;
            Revert_Recursive = recursive;

            if (callback != null)
                foreach (SourceItemCallbackData data in Revert__CallbackData)
                    callback(data.SourceItem, data.SourceItemResult);
        }

        public void Status(string localPath,
                           VersionSpec version,
                           bool recursive,
                           bool includeServer,
                           SourceItemCallback callback)
        {
            Status_LocalPath = localPath;
            Status_Recursive = recursive;
            Status_IncludeServer = includeServer;

            if (callback != null)
                foreach (SourceItemCallbackData data in Status__CallbackData)
                    callback(data.SourceItem, data.SourceItemResult);
        }

        public void Syncup(string localPath,
                           AddSourceItemCallback addItemCallback,
                           SyncupCallback syncupCallback)
        {
            Syncup_LocalPath = localPath;

            if (addItemCallback != null)
                addItemCallback(new SourceItem());

            if (syncupCallback != null)
                syncupCallback(new SourceItem(), SyncupAction.None, SourceItemResult.S_Ok);
        }

        public void Update(string localPath,
                           bool recursive,
                           VersionSpec version,
                           UpdateCallback callback)
        {
            Update_LocalPath = localPath;
            Update_Recursive = recursive;
            Update_Version = version;

            if (callback != null)
                callback(new SourceItem(), UpdateAction.Updated, SourceItemResult.S_Ok);
        }
    }
}